﻿@using ZX.Model
@using ZX.Tools
@using ZX.BLL
@using System.Linq
@using System.Text

@model List<Project_TaskModel>
@{
    ViewBag.Title = "任务报表-指效OA";
    var StrList = ViewBag.StrList as List<Project_TaskModel>;
    var intpage = Model.Count();
    var inpage = ViewBag.result;
    DateTime? currentTime = ViewBag.CurrentTime;
}

<style>
    .label.task-status-doing {
        color: #D2322D
    }

    .label.task-status-done {
        color: #229F24
    }

    .label.task-status-waiting {
        color: #E48600
    }

    .label.task-status-cancel {
        color: #333
    }

    .taskMenu {
        width: 200px
    }

        .taskMenu + .page-content {
            margin-left: 220px;
        }

    #dropMenu {
        min-width: 200px;
    }

    #defaultMenu, #finishedMenu, #suspendMenu {
        min-width: 168px;
    }

    #groupBar .btn-group {
        margin-right: 15px;
    }

    #currentItem {
        margin-right: 1px;
    }

    .children {
        background: #f0f0f0;
    }

    .modal-team .table-form {
        margin: auto;
        width: 90%;
    }

    .modal-team .modal-content {
        padding: 5px;
    }

        .modal-team .modal-content .table-form td + td, .table-form th + th {
            padding-left: 5px;
        }

    .panel .panel-heading a:hover {
        text-decoration: none;
    }

    #menu .pull-right {
        margin-right: 5px;
    }

    .page-content > .panel > .panel-heading > .panel-actions {
        margin-left: -5px;
    }

    .tr-child, .tr-child > td {
        padding: 0;
    }

        .tr-child > td > table {
            padding: 5px;
            background-color: #fff;
            border: 2px solid #DDDDDD;
        }

    td.wait {
        color: #808080;
    }

    td.doing {
        color: #d2322d;
    }

    td.done {
        color: #229f24;
    }

    td.closed {
        color: #E48600;
    }

    td.cancel {
        color: #888888;
    }

    .w-290px {
        width: 12.5%;
        /*font-size: 12.8;*/
    }

    .circular {
        width: 20px;
        height: 20px;
        border-radius: 50%;
        -moz-border-radius: 50%;
        -webkit-border-radius: 50%;
        float: left;
        margin-right: 5px;
    }

    .red {
        background-color: red;
    }

    .green {
        background-color: green;
    }

    .Orange {
        background-color: yellow;
    }
</style>
<script language='Javascript'>v.lang = { "confirmDelete": "\u60a8\u786e\u5b9a\u8981\u6267\u884c\u5220\u9664\u64cd\u4f5c\u5417\uff1f", "confirmFinish": "\u60a8\u786e\u5b9a\u8981\u6267\u884c\u5b8c\u6210\u64cd\u4f5c\u5417\uff1f", "deleteing": "\u5220\u9664\u4e2d", "doing": "\u5904\u7406\u4e2d", "timeout": "\u7f51\u7edc\u8d85\u65f6,\u8bf7\u91cd\u8bd5", "confirmDiscardChanges": "\u8868\u5355\u5df2\u66f4\u6539\uff0c\u786e\u5b9a\u5173\u95ed\uff1f", "yes": "\u662f", "no": "\u5426" };</script>


<input type="hidden" id="projectId" value="@ViewBag.ProjectId" />
<input type="hidden" name="currentMid" id="currentMid" value="@ViewBag.CurrentMid" />
<input type="hidden" id="secondMenuId" name="secondMenuId" value="@ViewBag.SecondMenuId" />

<form method="post" action="/Project_Task/Project_TaskManage?mid=@ViewBag.CurrentMid&secondMenuId=@ViewBag.SecondMenuId" id="myForm">
    <input type="hidden" name="pageIndex" id="pageIndex" value="@ViewBag.PageIndex" />
    <input type="hidden" name="pageSize" id="pageSize" value="@ViewBag.PageSize" />
    <input type="hidden" name="count" id="count" value="@ViewBag.Count" />
    <input type="hidden" name="status" id="status" value="@Request.Form["status"]" />
    <input type="hidden" name="selectType" id="selectType" value="@Request.Form["selectType"]" />
</form>

<div class='row page-content with-menu' style="margin-top:20px">
    <div id='menuActions'>
        @Html.Raw(ViewBag.CurrentBtnList32)
    </div>
    <script language='Javascript'>v.mode = "finishedBy";</script>
    <script language='Javascript'>v.projectID = "";</script>
    <div class='row with-menu page-content'>
        <div class='panel'>
            <div class='panel-heading'>
                <strong><i class='icon-list-ul'></i>任务报表</strong>
            </div>
            <div class="panel-body">
                <div>
                    <form action="OutExcel" id="doForm">
                        <fieldset>
                            <legend>查询条件</legend>
                            <div class="form-inline">
                                <div class="form-group">
                                    <label for="exampleInputEmail3">日期</label>
                                    <input type="text" class="form-control" id="StartTime" placeholder="截止日期" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" style="width:120px;" />
                                </div>
                                <div class="form-group">
                                    <label for="exampleInputInviteCode3">状态</label>
                                    <select id="status" class="form-control">
                                        <option value='' data-keys=' '></option>
                                        <option value='2'>未开始</option>
                                        <option value='3'>进行中</option>
                                        <option value='4'>已完成</option>
                                        <option value='5'>已取消</option>
                                        <option value='6'>已关闭</option>
                                    </select>
                                </div>
                                <button type="button" class="btn btn-default" id="btnSearch">查询</button>
                                <div style="width:auto;float:right;">
                                    <a href='javascript:;' onclick="exportExcel()" class="btn btn-primary">导出Excel</a>
                                </div>
                            </div>
                        </fieldset>
                    </form>
                </div>
                <table class='table table-hover table-striped table-bordered tablesorter table-data table-fixed' id='taskList'>
                    <thead>
                        <tr class='text-center'>
                            <th class='w-290px'>
                                <div>
                                    <a href='javascript:;'>编号</a>
                                </div>
                            </th>
                            <th class='w-290px'>
                                <div>
                                    <a href='javascript:;'>所属项目</a>
                                </div>
                            </th>

                            <th class='w-290px'>
                                <div>
                                    <a href='javascript:;'>名称</a>
                                </div>
                            </th>
                            <th class='w-290px'>
                                <div>
                                    <a href='javascript:;'>完成时间</a>
                                </div>
                            </th>
                            <th class='w-290px'>
                                <div>
                                    <a href='javascript:;'>完成人</a>
                                </div>
                            </th>
                            <th class='w-290px'>
                                <div class='headerSortUp'>
                                    <a href='javascript:;'>任务描述</a>
                                </div>
                            </th>
                            <th class='w-290px'>
                                <div>
                                    <a href='javascript:;'>创建日期</a>
                                </div>
                            </th>
                            <th class='w-290px'>
                                <div>
                                    <a href='javascript:;'>创建人</a>
                                </div>
                            </th>
                        </tr>

                    </thead>
                    <tbody>
                        @{
                            List<Project_TaskModel> list = Model;
                            if (list != null)
                            {
                                List<Project_Task_Team> task_team_List = new List<Project_Task_Team>();
                                List<Project_TaskModel> childList = new List<Project_TaskModel>();
                                List<Sys_User> listUser = ViewBag.ListUser;
                                int index = 1;
                                int pageIndex = ViewBag.PageIndex;
                                int pageSize = ViewBag.PageSize;
                                int tableRowNumber = 1;
                                foreach (var item in list)
                                {
                                    if (item.ParentId > 0)
                                    {
                                        continue;
                                    }
                                    //获取该任务所具有的所有任务团队子表记录,便于后续的耗时统计
                                    //先获取主任务关联的子表记录
                                    childList = list.Where(l => l.ParentId == item.Id).ToList();
                                    tableRowNumber = (pageIndex - 1) * pageSize + index++;
                                    <tr class='text-center'>
                                        <td class='text-left'><label class='checkbox-inline'><input type='checkbox' name='taskIDList[]' value='@item.Id' />@tableRowNumber </label></td>
                                        @{
                                            string projectName = "";//所属项目名

                                            if (item.ProjectName.Length > 5)
                                            {
                                                projectName = item.ProjectName.Substring(0, 5) + "...";
                                            }
                                            else
                                            {
                                                projectName = item.ProjectName;
                                            }
                                        }
                                        <td class='w-100px' title="@item.ProjectName">@projectName</td>

                                        <td class='text-left' title="@item.TName">
                                            @{
                                                //任务结束前一天 亮黄灯控制
                                                TimeSpan ts = Convert.ToDateTime(item.AsTime).Subtract(Convert.ToDateTime(currentTime.ToDateFormat("yyyy-MM-dd 00:00:00")));

                                                if (ts.Days == 1 && item.TState != 4)
                                                {
                                                    <div class="circular Orange" title="仅剩一天时间了!"></div>
                                                }
                                                if (ts.Days == 0 && item.TState != 4)
                                                {
                                                    <div class="circular Orange" title="即将超时"></div>
                                                }
                                                //超时
                                                if (ts.Days < 0 && item.TState != 4)
                                                {
                                                    <div class="circular red" title="已超时@(ts.Days*-1)天"></div>
                                                }
                                            }
                                            <label>@item.TName</label>
                                            @{
                                                if (childList.Count > 0)
                                                {
                                                    <span class="task-toggle">&nbsp;&nbsp;<i class="icon icon-minus"></i>&nbsp;&nbsp;</span>
                                                }
                                                string asTimeStr = "0000-00-00";
                                                string assignedRealName = "";
                                                try
                                                {
                                                    if (item.AsTime.IsNotNullOrEmpty())
                                                    {
                                                        asTimeStr = item.AsTime.ToShortDate();
                                                    }

                                                    if (item.Assigned.ToInt() > 0)
                                                    {
                                                        assignedRealName = listUser.Where(l => l.Id == item.Assigned.ToInt()).FirstOrDefault().RealName;
                                                    }
                                                }
                                                catch { }
                                            }
                                        </td>

                                        <td>@asTimeStr</td>
                                        <td>@assignedRealName</td>
                                        @{
                                            switch (item.TState)
                                            {
                                                case 1:
                                                    {
                                                        <td class="wait">未完成</td>
                                                        break;
                                                    }
                                                case 2:
                                                    {
                                                        <td class="wait">未开始</td>

                                                        break;
                                                    }
                                                case 3:
                                                    {
                                                        <td class="doing">进行中</td>
                                                        break;
                                                    }
                                                case 4:
                                                    {
                                                        <td class="done">已完成</td>
                                                        break;
                                                    }
                                                case 5:
                                                    {
                                                        <td class="cancel">已取消</td>
                                                        break;
                                                    }
                                                case 6:
                                                    {
                                                        <td class="closed">已关闭</td>
                                                        break;
                                                    }

                                                default:
                                                    <td>未知</td>
                                                    break;
                                            }
                                            List
                                            <Project_Task_Team>
                                            taskTeamList = new List<Project_Task_Team>
                                            ();

                                            var consumTime = 0;
                                            var tExpected = 0;
                                            string pCreateTime = "0000-00-00";
                                            if (item.CreateTime.IsNotNullOrEmpty())
                                            {
                                                pCreateTime = item.CreateTime.ToShortDate();
                                            }
                                        }
                                        <td>@pCreateTime</td>
                                        @if (childList.Count <= 0)
                                        {
                                            taskTeamList = Project_Task_TeamBLL.GetList(d => d.Where(t => t.FK_TaskId == item.Id));
                                            consumTime = taskTeamList.Sum(d => d.ConsumTime).ToInt();
                                            tExpected = taskTeamList.Sum(d => d.TheTime).ToInt();
                                        }
                                        else
                                        {
                                            StringBuilder childIds = new StringBuilder();

                                            foreach (var childItem in childList)
                                            {
                                                childIds.Append(childItem.Id);
                                                if (childItem != childList.Last())
                                                {
                                                    childIds.Append(",");
                                                }
                                            }
                                            taskTeamList = Project_Task_TeamBLL.GetList(" FK_TaskId in(" + childIds + ")");
                                            consumTime = taskTeamList.Sum(d => d.ConsumTime).ToInt();
                                            tExpected = taskTeamList.Sum(d => d.TheTime).ToInt();

                                        }
                                        <td>@item.CreateAccount</td>

                                    </tr>

                                    if (childList != null)
                                    {
                                        <tr class='tr-child' style="display: table-row;">
                                            <td colspan='11'>
                                                <table class='table table-data table-hover table-fixed'>
                                                    @{
                                                        int j = 1;
                                                        foreach (var childItem in childList)
                                                        {
                                                            <tr class='text-center'>
                                                                <td class='w-60px'>@tableRowNumber-@j</td>

                                                                <td class='w-100px' title="@item.ProjectName">@projectName</td>
                                                                <td class='w-40px'><span class='active pri pri-@item.Priority'>@item.Priority</span></td>

                                                                <td class='text-left' title="@item.TName">
                                                                    @{
                                                                        //任务结束前一天 亮黄灯控制
                                                                        TimeSpan tsChild = Convert.ToDateTime(childItem.AsTime).Subtract(Convert.ToDateTime(currentTime.ToDateFormat("yyyy-MM-dd 00:00:00")));

                                                                        if (tsChild.Days == 1 && childItem.TState != 4)
                                                                        {
                                                                            <div class="circular Orange" title="仅剩一天时间了!"></div>
                                                                        }
                                                                        if (ts.Days == 0 && childItem.TState != 4)
                                                                        {
                                                                            <div class="circular Orange" title="即将超时"></div>
                                                                        }
                                                                        //超时
                                                                        if (tsChild.Days < 0 && childItem.TState != 4)
                                                                        {
                                                                            <div class="circular red" title="已超时@(tsChild.Days*-1)天"></div>
                                                                        }
                                                                    }
                                                                    <span class='label'>子</span>
                                                                    <a href="javascript:;">@childItem.TName</a>
                                                                </td>
                                                                @{
                                                                    string childAsTimeStr = "0000-00-00";
                                                                    string childAssignedRealName = "";
                                                                    try
                                                                    {
                                                                        if (childItem.AsTime.IsNotNullOrEmpty())
                                                                        {
                                                                            childAsTimeStr = childItem.AsTime.ToShortDate();
                                                                        }

                                                                        if (childItem.Assigned.ToInt() > 0)
                                                                        {
                                                                            childAssignedRealName = listUser.Where(l => l.Id == childItem.Assigned.ToInt()).FirstOrDefault().RealName;
                                                                        }
                                                                    }
                                                                    catch { }

                                                                }
                                                                <td class='w-100px'>@childAsTimeStr</td>
                                                                <td class='w-80px'>@childAssignedRealName</td>

                                                                @switch (childItem.TState)
                                                                {
                                                                    case 1:
                                                                        {
                                                                            <td class="w-90px wait">未完成</td>
                                                                            break;
                                                                        }
                                                                    case 2:
                                                                        {
                                                                            <td class="w-90px wait">未开始</td>

                                                                            break;
                                                                        }
                                                                    case 3:
                                                                        {
                                                                            <td class="w-90px doing">进行中</td>
                                                                            break;
                                                                        }
                                                                    case 4:
                                                                        {
                                                                            <td class="w-90px done">已完成</td>
                                                                            break;
                                                                        }
                                                                    case 5:
                                                                        {
                                                                            <td class="w-90px cancel">已取消</td>
                                                                            break;
                                                                        }
                                                                    case 6:
                                                                        {
                                                                            <td class="w-90px closed">已关闭</td>
                                                                            break;
                                                                        }

                                                                    default:
                                                                        <td class="w-90px wait">未知</td>
                                                                        break;

                                                                }
                                                                @{
                                                                    var childConsumTime = taskTeamList.Where(d => d.FK_TaskId == childItem.Id.ToInt()).Sum(t => t.ConsumTime);
                                                                    var childTExpected = taskTeamList.Where(d => d.FK_TaskId == childItem.Id.ToInt()).Sum(t => t.TheTime);
                                                                    string childCreateTime = "0000-00-00";
                                                                    if (childItem.CreateTime.IsNotNullOrEmpty())
                                                                    {
                                                                        childCreateTime = childItem.CreateTime.ToShortDate();
                                                                    }
                                                                }

                                                                <td class='w-90px'>@childCreateTime</td>
                                                                <td class='w-80px'>@childConsumTime</td>
                                                                <td class='w-100px'>@childTExpected</td>

                                                            </tr>
                                                            j++;
                                                        }
                                                    }
                                                </table>
                                            </td>
                                        </tr>
                                    }
                                }
                            }
                        }
                    </tbody>
                </table>

                <div class="table-footer">
                    <div class="pull-left text-danger"></div>
                    <div style="float:right; " class="pager form-inline">
                        <div id="myPager">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src='~/res/js/jquery/form/min.js?v=4.6.2' type='text/javascript'></script>
<script language='Javascript'>
    $(function () {
        if (v.backLink !== undefined) $('#menu .nav:first').append('<li>' + v.backLink + '</li>');
        if ($.cookie('projectStatus') && v.projectID) {
            $('#mainNavbar .nav li').removeClass('active').find('[href*=' + $.cookie('projectStatus') + ']').parent().addClass('active');
        }

        /* Set style of priority options in form */
        $('form .pri[data-value="' + $('form #pri').val() + '"]').addClass('active');
        $('form .pri').click(function () {
            $('form .pri.active').removeClass('active');
            $('form #pri').val($(this).addClass('active').data('value'));
        });

        $('#menu li[data-group="' + v.groupBy + '"]').addClass('active');

        $('.task-toggle').click(function () {
            var obj = $(this).find('i');
            if (obj.hasClass('icon-plus')) {
                obj.parents('tr').next('tr').show();
                obj.removeClass('icon-plus').addClass('icon-minus');
            }
            else if (obj.hasClass('icon-minus')) {
                obj.parents('tr').next('tr').hide();
                obj.removeClass('icon-minus').addClass('icon-plus');
            }
            return false;
        });


        $('.switcher').on('click', function () {
            var url = $(this).attr('href');
            bootbox.confirm($(this).data('confirm'), function (result) {
                if (result) {
                    $.getJSON(url, function (response) {
                        if (response.result == 'success') {
                            bootbox.alert(response.message, function () { location.reload() });
                        }
                        else {
                            bootbox.alert(response.message);
                        }
                    })
                }
            })
            return false;
        });
        initPage();
        adjustSortBtn();
    });

    function adjustSortBtn() {
        $('.btn-move-up:first').addClass('disabled').attr('disabled', 'disabled');
        $('.btn-move-down:last').addClass('disabled').attr('disabled', 'disabled');
    }

    function setComment() {
        $('#commentBox').toggle();
        $('.ke-container').css('width', '100%');
        setTimeout(function () { $('#commentBox textarea').focus(); }, 50);
    }
    $(function () {
        $('#menu .nav > li').removeClass('active');
        if (v.mode) $('#menu .nav').find('[href*=' + v.mode + ']').parent().addClass('active');
        else $('#menu .nav').find('li.all').addClass('active');
    });


    //创建任务
    function addTask() {
        window.location.href = "/Project_Task/Add?id=0&mid=" + $("#currentMid").val() + "&projectId=" + $("#projectId").val() + "&secondMenuId=" + $("#secondMenuId").val();
    }
    function look(obj) {
        var taskInfo = $(obj).attr("val");
        var tArray = taskInfo.split(',');
        win.open("/Project_Task/Readonly?id=" + tArray[0] + "&mid=" + $("#currentMid").val() + "&secondMenuId=" + $("#secondMenuId").val(), 1024, 700);
    }
    //编辑
    function editTask(obj) {
        var taskInfo = $(obj).attr("val");
        var tArray = taskInfo.split(',');
        window.location.href = "/Project_Task/Edit?id=" + tArray[0] + "&mid=" + $("#currentMid").val() + "&secondMenuId=" + $("#secondMenuId").val();
    }
    //工时
    function workTimeTask(obj) {
        var taskInfo = $(obj).attr("val");
        var tArray = taskInfo.split(',');
        win.open("/Project_Task/WorkTime?taskId=" + tArray[0] + "&consumTime=" + tArray[1] + "&theTime=" + tArray[2] + "&secondMenuId=" + $("#secondMenuId").val(), 770, 370, tArray[3] + ".工时");
    }

    //开始
    function runTask(obj) {
        var taskInfo = $(obj).attr("val");
        var tArray = taskInfo.split(',');
        win.open("/Project_Task/Run?taskId=" + tArray[0] + "&consumTime=" + tArray[1] + "&theTime=" + tArray[2] + "&secondMenuId=" + $("#secondMenuId").val(), 770, 420, tArray[3] + ".开始");
    }
    //指派
    function appointTask(obj) {
        var taskInfo = $(obj).attr("val");
        var tArray = taskInfo.split(',');
        win.open("/Project_Task/AppointTask?taskId=" + tArray[0] + "&consumTime=" + tArray[1] + "&theTime=" + tArray[2] + "&secondMenuId=" + $("#secondMenuId").val(), 770, 370, tArray[3] + ".指派");
    }
    //完成
    function finishTask(obj) {
        var taskInfo = $(obj).attr("val");
        var tArray = taskInfo.split(',');
        win.open("/Project_Task/Finish?taskId=" + tArray[0] + "&consumTime=" + tArray[1] + "&theTime=" + tArray[2] + "&secondMenuId=" + $("#secondMenuId").val(), 770, 450, tArray[3] + ".完成");
    }
    //子任务
    function addChildTask(obj) {
        var taskInfo = $(obj).attr("val");
        var tArray = taskInfo.split(',');
        win.open("/Project_Task/Children?taskId=" + tArray[0] + "&consumTime=" + tArray[1] + "&theTime=" + tArray[2] + "&secondMenuId=" + $("#secondMenuId").val(), 770, 450, tArray[3] + ".子任务");
    }

    //关闭
    function closeTask(obj) {
        var taskInfo = $(obj).attr("val");
        var tArray = taskInfo.split(',');
        win.open("/Project_Task/Close?taskId=" + tArray[0] + "&consumTime=" + tArray[1] + "&theTime=" + tArray[2] + "&secondMenuId=" + $("#secondMenuId").val(), 770, 270, tArray[3] + ".完成");
        //win.open("/Project_Task/Close?taskId=" + fk_TaskId + "&fk_ProjectId=" + fk_ProjectId + "&expectHours=" + expectHours + "&secondMenuId=" + $("#secondMenuId").val(), 770, 270, taskName + ".关闭");
    }

    //进入任务
    function goProject_Task(projectId) {
        window.location.href = "/Project_Task/GetProjectTaskList?projectId=" + projectId + "&mid=" + $("#currentMid").val() + "&secondMenuId=" + $("#secondMenuId").val();
    }

    //我的任务
    function IndividualTasks(mark) {
        window.location.href = "/Project_Task/GetProject_TaskList?mark=" + mark + "&secondMenuId=" + $("#secondMenuId").val();
    }

    //显示项目信息
    function openProjectInfo(projectId) {
        win.open = "/Project/AddEdit?id=" + projectId + "&secondMenuId=" + $("#secondMenuId").val();
    }


    //进入任务
    function goProject_Task(projectId) {

        if (projectId == 0 || projectId == null || projectId == undefined) {
            return;
        }
        window.location.href = "/Project_Task/GetProjectTaskList?projectId=" + projectId + "&mid=" + $("#currentMid").val();
    }

    //状态显示
    function IndividualTasks(type) {
        $("#selectType").val(type);
        $("#status").val();
        $("#myForm").submit();
    }
    //配置分页
    function initPage() {
        var pageIndex = $("#pageIndex").val();
        var pageSize = $("#pageSize").val();
        var count = $("#count").val();
        layui.use(['laypage', 'layer'], function () {
            var laypage = layui.laypage
                , layer = layui.layer;
            //完整功能
            laypage.render({
                elem: 'myPager'
                , limit: pageSize
                , curr: pageIndex
                , count: count
                , theme: '#1E9FFF'
                , layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
                , jump: function (obj, first) {
                    if (!first) {
                        pageIndex = obj.curr;
                        pageSize = obj.limit;
                        $("#pageIndex").val(obj.curr);
                        $("#pageSize").val(obj.limit);
                        $("#myForm").submit();
                    }
                }
            });
        });
    }
    //显示项目信息
    function openProjectInfo(projectId) {
        win.open.href("/Project/AddEdit?id=" + projectId);
    }

    function exportExcel() {
        $("#doForm").submit();
    }
</script>
